package com.advtower.towerapi.controller;

import com.advtower.towerapi.dao.OfferMongoDbDao;
import com.advtower.towerapi.entity.OfferMongoEntity;
import com.advtower.towerapi.utils.HttpUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author herman
 * @version 1.0
 * @date 2021/12/3 21:51
 */
@Controller
@RequestMapping("/offer")
public class OfferController {

    @Autowired
    OfferMongoDbDao offerMongoDbDao;

    @RequestMapping("sync")
    @ResponseBody
    public String sync(){
        String apiResult = null;
        try {
            //从mysql读取广告主信息包括url，apiResult map等
            //TODO
            String url = "http://api.furthermoreads.com/v2/offer/pull?token=a4b596524e5d4687adbd8085fbacf02f";
            apiResult = HttpUtils.httpGet(url);
            HashMap apiResultMap = JSON.parseObject(apiResult,HashMap.class);
            List<Map> offerList = (List)apiResultMap.get("data");
            for (Map offerMap : offerList) {
                System.out.println(offerMap.get("app_pack_name"));
            }
            OfferMongoEntity offerMongoEntity = new OfferMongoEntity();
            offerMongoEntity.setId("123123");
            offerMongoDbDao.save(offerMongoEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return apiResult;
    }
}
